import { usePermissionStore } from '@/stores/permission'

const handler = (el: HTMLElement, binding: any) => {
  if (binding.value?.toString() === binding.oldValue?.toString()) return

  const { permissions } = storeToRefs(usePermissionStore())
  let hasPermission = false

  if (binding.value instanceof Array) {
    if (binding.arg === '&&') {
      hasPermission = binding.value.every((item: any) => permissions.value.includes(item))
    } else {
      hasPermission = binding.value.some((item: any) => permissions.value.includes(item))
    }
  }

  !hasPermission && el.remove()
}

export default handler

//useage
// v-permission="[permissionCode,...]" 包含数组中一个权限即可
// v-permission:&&="[permissionCode,...]" 需包含数组中所有权限
